package com.dongdongshop.controller;


import cn.hutool.json.JSONUtil;
import com.dongdongshop.api.SellerServiceApi;
import com.dongdongshop.data.Result;
import com.dongdongshop.dto.SellerDTO;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.enums.TokenEnum;
import com.dongdongshop.exception.LoginException;
import com.dongdongshop.model.TbAdmin;
import com.dongdongshop.shiro.LoginToken;
import com.dongdongshop.util.JwtUtil;
import com.dongdongshop.util.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private SellerServiceApi sellerServiceApi;

    @PostMapping("/login")
    public Result login(@RequestParam String userName, @RequestParam String passWord){
        Subject subject = SecurityUtils.getSubject();
        LoginToken loginToken = new LoginToken(userName,passWord,TokenEnum.ADMIN);
        try {
            subject.login(loginToken);
        }catch (UnknownAccountException e1 ){
            return Result.RESPOSE(ResultEnum.UNKNOW_USER_NAME_PASSWORD);
        }catch (IncorrectCredentialsException e2){
            return Result.RESPOSE(ResultEnum.UNKNOW_USER_NAME_PASSWORD);
        }
        //单点登陆 客户端保存登录状态
        TbAdmin admin =(TbAdmin) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(admin.getUid().toString());
        loginUser.setUserName(admin.getUname());
        //加密
        String token = jwtUtil.createJwt(loginUser.getUserId(), JSONUtil.toJsonStr(loginUser),"loginUser");
       return Result.OK().setData(token);
    }

    @PostMapping("/sellerLogin")
    public Result sellerLogin(@RequestParam String userName, @RequestParam String passWord){
        Subject subject = SecurityUtils.getSubject();
        LoginToken usernamePasswordToken = new LoginToken(userName,passWord, TokenEnum.SELLER);
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e1 ){
            return Result.RESPOSE(ResultEnum.UNKNOW_USER_NAME_PASSWORD);
        }catch (IncorrectCredentialsException e2){
            return Result.RESPOSE(ResultEnum.UNKNOW_USER_NAME_PASSWORD);
        }catch (LoginException e3){
            return Result.RESPOSE(ResultEnum.ACCOUNT_STATUS);
        }
        //单点登陆 客户端保存登录状态
        SellerDTO seller =(SellerDTO) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(seller.getSellerId());
        loginUser.setUserName(seller.getName());
        //加密
        String token = jwtUtil.createJwt(loginUser.getUserId(), JSONUtil.toJsonStr(loginUser),"loginUser");
        return Result.OK().setData(token);
    }

    @PostMapping("/checkName")
    public Result checkName(@RequestParam String sellerId){
        Result<SellerDTO> result = sellerServiceApi.getSellerByName(sellerId);
        SellerDTO sellerDTO = result.getData();
        if(sellerDTO == null){
            return Result.OK();
        }
        return Result.RESPOSE(ResultEnum.NAME_ALREAD_REGISTER);
    }

}
